/**
 * Created by Reid on 2020-04-14 16:02
 *
 * http请求
 *
 * Update by Reid on 2020-04-28 16:07
 *  在原有get post基础上增加 delete put 方法
 *  进一步整合接口 在方法中统一添加参数 现有参数: baseUrl  / postData
 *      @param:
 *          baseUrl: 基础接口名  直接从Url.js中引出的接口名
 *          postData: 接口请求数据的参数 如果是get接口,需要将参数添加到url中; 如果是post/delete/put接口,则放到payload中
 */

import React from "react"
import { getRootUrl } from './environment'
import { isEmpty } from '../utils/cmn'
import Cache from '../utils/Cache'
import fetch from 'isomorphic-fetch'
import { URL } from './enum'


// 请求加上跨域头
const generateOptions = options => {
	// if (!options.headers) options.headers = {}
	// options.headers = Object.assign(options.headers, {
	// 	'Access-Control-Allow-Origin': '*',
	// 	'Access-Control-Allow-Methods': '*',
	// 	'Access-Control-Allow-Headers': '*',
	// })
	return options;
}
// get
const baseGet = (baseUrl, postData) => {
	let userInfo = Cache.getUserInfo();
	baseUrl = String(baseUrl);
	let options = {method: 'get'}

	if (!isEmpty(userInfo)) {
		options.headers = {
			"Authorization": userInfo.key
		};
	}
	baseUrl = getRootUrl() + baseUrl

	options = generateOptions(options)
	// postData参数不为空 url拼接
	if (!isEmpty(postData)) {
		let index = 0;
		for (let key in postData) {
			baseUrl = baseUrl + (index+'' === '0'?'?'+key+'='+postData[key]:'&'+key+'='+postData[key])
			index++;
		}
	}

	const promiseObj = fetch(baseUrl, options)
	return promiseObj.then(
		resolve => {
			return resolve
		},
		reject => {
			return reject
		}
	)
}

export const Get = (baseUrl, postData={}) => {
	// 为data增加 _t 字段
	return baseGet(baseUrl, postData).then(
		response => {
			if (!response || response.status !== 200) {
				return {code: 1, msg: "请求错误"}
			}
			if(!response.jsonData)
				response.jsonData = response.json()
			return response.jsonData
		}
	).then(
		json => {
			return json
		}
	)
}

// post
const basePost = (baseUrl, postData) => {
	let userInfo = Cache.getUserInfo();

	// 生成options
	let options = {method: 'post'}
	options.headers = {"Content-Type": "application/json"}
	if (!isEmpty(userInfo)) {
		options.headers = {"Content-Type":"application/json", "Authorization": userInfo.key};
	}
	// 生成body
	if (!isEmpty(postData)) {
		options.body = JSON.stringify(postData)
	}
	baseUrl = getRootUrl() + String(baseUrl)
	const promiseObj = fetch(baseUrl, options);
	return promiseObj.then(
		resolve => {
			return resolve
		},
		reject => {
			return reject
		}
	)
}

export const Post = (baseUrl, postData= {}) => {
	return basePost(baseUrl, postData).then(
		response => {
			if (!response || response.status !== 200) {
				return {code: 1, msg: "请求错误"}
			}
			if(!response.jsonData)
				response.jsonData = response.json()
			return response.jsonData
		}
	).then(
		json => {
			return json
		}
	)
}

// delete
const baseDelete = (baseUrl, postData) => {
	let userInfo = Cache.getUserInfo();
	let options = {method: 'delete'}
	options.headers = {"Content-Type":"application/json"}
	if (!isEmpty(userInfo)) {
		options.headers = {"Content-Type":"application/json", "Authorization": userInfo.key};
	}
	// 生成body
	if (!isEmpty(postData)) {
		options.body = JSON.stringify(postData)
	}

	baseUrl = getRootUrl() + String(baseUrl)
	const promiseObj = fetch(baseUrl, options)
	return promiseObj.then(
		resolve => {
			return resolve
		},
		reject => {
			return reject
		}
	)
}

export const Delete = (baseUrl, postData= {}) => {
	return baseDelete(baseUrl, postData).then(
		response => {
			if (!response || response.status !== 202) {
				return {code: 1, msg: "请求错误"}
			}
			if(!response.jsonData)
				response.jsonData = response.json()
			return response.jsonData
		}
	).then(
		json => {
			return json
		}
	)
}

// put
const basePut = (baseUrl, postData) => {
	let userInfo = Cache.getUserInfo();

	// 生成options
	let options = {method: 'put'}
	options.headers = {"Content-Type":"application/json"}
	if (!isEmpty(userInfo)) {
		options.headers = {"Content-Type":"application/json", "Authorization": userInfo.key};
	}
	// 生成body
	if (!isEmpty(postData)) {
		options.body = JSON.stringify(postData)
	}
	baseUrl = getRootUrl() + String(baseUrl)
	const promiseObj = fetch(baseUrl, options);
	return promiseObj.then(
		resolve => {
			// console.log('resolve', resolve);
			return resolve
		},
		reject => {
			// console.log('reject', reject);
			return reject
		}
	)
}

export const Put = (baseUrl, postData= {}) => {
	return basePut(baseUrl, postData).then(
		response => {
			if (!response || (response.status !== 200 && response.status !== 202)) {
				return {code: 1, msg: "请求错误"}
			}
			if(!response.jsonData)
				response.jsonData = response.json()
			return response.jsonData
		}
	).then(
		json => {
			return json
		}
	)
}

export const Url = URL;
export const rootUrl = getRootUrl();
